package com.example.spring.multi.basic.dingdang;

import cn.hutool.poi.excel.ExcelReader;
import cn.hutool.poi.excel.ExcelUtil;
import com.example.spring.multi.basic.dingdang.model.StockExcelModel;

import java.io.File;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * @program: knowledge-pooling
 * @ClassName StockExcelDealUtil
 * @description: 库存Excel处理类
 * @author: tomato
 * @create: 2024-07-17 16:13
 * @Version 1.0
 **/
public class StockExcelDealUtil {
    //初始化城市库位排除列表
    public static Map<String, List<String>> CITY_STOCK = new HashMap<>();
    static {
        CITY_STOCK.put("郑州市", List.of("DCL", "THK", "ZYK"));
        CITY_STOCK.put("广州市", List.of("DCL", "DCL3"));
        CITY_STOCK.put("深圳市", List.of("DCL", "DCL1", "DCL2", "DCL3"));
    }
    /**
     * 仓库：库位为SJK的数据，英克去重累加可供量
     * 保留英克和可供量
     */
    public static Map<String, Integer> dealStockExcel(String cityName, File stockFile) {

        ExcelReader reader = ExcelUtil.getReader(stockFile);
        // 读取表头信息,并且将表头信息转换成对应的实体类
        reader.addHeaderAlias("英克ID", "inkId");
        reader.addHeaderAlias("库位", "locationName");
        reader.addHeaderAlias("可供量", "stockQuantity");
        List<StockExcelModel> dataList =  reader.readAll(StockExcelModel.class);
        // 关闭reader
        reader.close();
        // 处理数据
        Map<String, Integer> dealList = dealStockExcelData(cityName,dataList);
        return dealList;
    }

    private static Map<String, Integer> dealStockExcelData(String cityName, List<StockExcelModel> dataList) {
        Map<String, Integer> groupedStockQuantity = new HashMap<>();
        //筛选出库位为SKU的数据，根据英克Id去重累加可供量
        // 使用流API筛选、分组和累加库存数量
        if (CITY_STOCK.containsKey(cityName)) {
            List<String> strings = CITY_STOCK.get(cityName);
            groupedStockQuantity = dataList.stream()
                    .filter(item -> !strings.contains(item.getLocationName())) // 筛选库位名称为SKU的数据
                    .collect(Collectors.groupingBy(StockExcelModel::getInkId, // 按英克ID分组
                            Collectors.summingInt(StockExcelModel::getStockQuantity))); // 累加库存数量
        }else {
            if ("福州市".equals(cityName)){
                groupedStockQuantity = dataList.stream()
                        .collect(Collectors.groupingBy(StockExcelModel::getInkId, // 按英克ID分组
                                Collectors.summingInt(StockExcelModel::getStockQuantity))); // 累加库存数量
            }else {
                groupedStockQuantity = dataList.stream()
                        .filter(item -> "SJK".equals(item.getLocationName())) // 筛选库位名称为SKU的数据
                        .collect(Collectors.groupingBy(StockExcelModel::getInkId, // 按英克ID分组
                                Collectors.summingInt(StockExcelModel::getStockQuantity))); // 累加库存数量
            }
        }


        return groupedStockQuantity;
    }

}
